/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
/**
 * @description GLBalanceUtil.java
 * @author dongbin
 * @version 
 * @copyright
 */

package com.erp.ledger.balance.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.ledger.balance.dao.model.GLBalance;

/**
 * @description
 * @date 2021年2月3日
 * @author dongbin
 * 
 */
public class GLBalanceUtil {
    
    
    /**
     * 
     * @description 验证试算平衡表
     * @date 2021年2月3日
     * @author dongbin
     * @param glBalanceList（必须按时间递增或主键递增排序）
     * @return
     * @return String
     *
     */
    public static String validateBalanceList(List<GLBalance> glBalanceList) {
        //期初借贷合计
        BigDecimal beginDrSum = new BigDecimal(0D);
        BigDecimal beginCrSum = new BigDecimal(0D);
        //本期借贷合计
        BigDecimal drSum = new BigDecimal(0D);
        BigDecimal crSum = new BigDecimal(0D);
        //期末借贷合计
        BigDecimal endDrSum = new BigDecimal(0D);
        BigDecimal endCrSum = new BigDecimal(0D);
        //根据coa重新梳理的平衡表
        Map<String, List<GLBalance>> coaBalanceMap = new HashMap<String, List<GLBalance>>();
        
        //循环重新梳理coa平衡表
        for(GLBalance balanceTemp: glBalanceList) {
            if(coaBalanceMap.containsKey(balanceTemp.getSubjectCode())) {
                coaBalanceMap.get(balanceTemp.getSubjectCode()).add(balanceTemp);
            }else {
                List<GLBalance> balanceListTemp = new ArrayList<GLBalance>();
                balanceListTemp.add(balanceTemp);
                coaBalanceMap.put(balanceTemp.getSubjectCode(), balanceListTemp);
            }
        }
        
        //循环coa平衡表MAP计算合计
        for(String key: coaBalanceMap.keySet()) {
            //获取某一科目结构的余额明细
            List<GLBalance> balanceList = coaBalanceMap.get(key);
            //循环明细
            for(int a=0;a<balanceList.size();a++) {
                //计算期初合计
                if(a==0) {
                    beginDrSum = beginDrSum.add(new BigDecimal(balanceList.get(a).getBeginDr()));
                    beginCrSum = beginCrSum.add(new BigDecimal(balanceList.get(a).getBeginCr()));
                }
                //计算期末合计
                if(a==balanceList.size()-1) {
                    endDrSum = endDrSum.add(new BigDecimal(balanceList.get(a).getBeginDr())).add(new BigDecimal(balanceList.get(a).getPeriodDr()));
                    endCrSum = endCrSum.add(new BigDecimal(balanceList.get(a).getBeginCr())).add(new BigDecimal(balanceList.get(a).getPeriodCr()));
                }
                //计算本期发生合计
                drSum = drSum.add(new BigDecimal(balanceList.get(a).getPeriodDr()));
                crSum = crSum.add(new BigDecimal(balanceList.get(a).getPeriodCr()));
            }
        }
        
        String result = "";
        
        //验证平衡表是否平衡
        if(beginDrSum.doubleValue()!=beginCrSum.doubleValue()) {
            result = result + "期初余额不平衡,";
        }
        if(drSum.doubleValue()!=crSum.doubleValue()) {
            result = result + "本期发生额不平衡,";
        }
        if(endDrSum.doubleValue()!=endCrSum.doubleValue()) {
            result = result + "期末余额不平衡,";
        }
        
        return result;
    }

}
